﻿// -----------------------------------------
// 2020-10-8
// wng0@sina.com
// 整合步兵与骑兵
//------------------------------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class Role : MonoBehaviour
{
    public RoleCatagory m_roleCatagory=RoleCatagory.Rider; // 角色分类, 在编辑器界面进行初始化
    float m_life;    // 生命值
    float m_speed;   // 移动速度    
    NavMeshAgent m_agent;
    Vector3 m_destinationPosition;    
    void Start()
    {
        m_agent=this.GetComponent<NavMeshAgent>();
        SetupRole();
    }
    void Update()
    {
        CalculateLife();
        Move();
    }
    void CalculateLife()    //进行生命值计算,死亡销毁
    {
        if (m_life<=0)
        {
            PoolManager.Instance.Despawn(this.gameObject);
        }
    }
    void Move()             //进行位置更新
    {
        if (m_life<=0)
        {
            return;
        }
        if (m_roleCatagory==RoleCatagory.Walker)
        {
            m_destinationPosition=PoolManager.Instance.m_TargetPosition.transform.position;
        }
        else if (m_roleCatagory==RoleCatagory.Rider)
        {
            m_destinationPosition=PoolManager.Instance.m_Flag.transform.position;
        }
        m_agent.speed=m_speed;
        m_agent.SetDestination(m_destinationPosition);
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag=="Arrow")
        {
            Debug.Log(this.gameObject.name+other.GetComponent<Arrow>().targetTag);
            if (other.GetComponent<Arrow>().targetTag==m_roleCatagory)
            {
                Debug.Log("was attacked");
                m_life-=1;
            }
        }
    }
    void SetupRole()
    {
        if (m_roleCatagory==RoleCatagory.Rider)
        {
            m_life=1;
            m_speed=3;
        }
        else if (m_roleCatagory==RoleCatagory.Walker)
        {
            m_life=2;
            m_speed=1;
        }
    }    
}
public enum RoleCatagory //角色分类, 用于在编辑器界面进行选择
{
    Walker,
    Rider,
    None
}